import { _decorator, Component, Label, Node } from 'cc';
import { AudioMgr, SoundName } from '../Mgr/AudioMgr';
import { UIMgr } from '../Mgr/UIMgr';
import { AssetList, Global } from '../Global';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
import { GAME_MODE, GAMEDATA } from '../GameScene/GAMEDATA';
const { ccclass, property } = _decorator;

@ccclass('GameEnd')
/**
 * 游戏结束层
 */
export class GameEnd extends Component {

    //游戏分数
    @property(Label)
    public Score: Label = null;

    //最高分数
    @property(Label)
    private BestScore: Label = null;

    /**
     * 更新游戏分数
     */
    public UpdateScore(_score: number) {
        this.Score.string = _score.toString();

        switch (GAMEDATA.GAME_MODE) {
            case GAME_MODE.NORMAL:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.NORMAL_SCORE.toString();

                if (_score > LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.NORMAL_SCORE) {//更新本地分数
                    LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.NORMAL_SCORE = _score;
                    LocalStorageMgr.Instance.UpdateLocalStroage();
                }
                break;
            case GAME_MODE.CHANGE:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CHANGE_SCORE.toString();

                if (_score > LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CHANGE_SCORE) {//更新本地分数
                    LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CHANGE_SCORE = _score;
                    LocalStorageMgr.Instance.UpdateLocalStroage();
                }
                break;
            case GAME_MODE.TIME:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.TIME_SCORE.toString();

                if (_score > LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.TIME_SCORE) {//更新本地分数
                    LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.TIME_SCORE = _score;
                    LocalStorageMgr.Instance.UpdateLocalStroage();
                }
                break;
        }

    }

    /**
     * 点击回到首页按钮
     */
    public GoLogin() {
        AudioMgr.instance.PlayEffect(SoundName.Click);
        UIMgr.Instance.HidePop(AssetList.Pop.GAMEEND);
        UIMgr.Instance.UdpateScene(AssetList.Scene.LoginScene);
    }

}


